//
//  TSHomePresenter.m
//  TaShuo
//
//  Created by N-X on 2018/1/17.
//  Copyright © 2018年 MRNAN. All rights reserved.
//

#import "TSHomePresenter.h"
#import "TSHotAreaModel.h"
#import "TSCityCommentModel.h"

@interface TSHomePresenter()
@property (nonatomic ,strong)NSMutableArray * mHeaderImageArray;
@property (nonatomic ,strong)NSMutableArray <TSHotAreaPresenter *>* mHotAreaArray;
@property (nonatomic ,strong)NSMutableArray <TSCityCommentPresenter *>* mCityCommentArray;
@end
@implementation TSHomePresenter

- (instancetype)init
{
    if (self = [super init]) {
        
    }
    return self;
}

- (NSArray *)headerImageArray
{
    return self.mHeaderImageArray;
}

- (NSArray<TSHotAreaPresenter *> *)hotAreaArray
{
    return self.mHotAreaArray;
}

- (NSArray<TSCityCommentPresenter *> *)cityCommentArray
{
    return self.mCityCommentArray;
}

- (void)refreshData
{
    [self fetchDataWithCompletionHandle:nil];
}

- (void)loadMoreData
{
    [[TSNetTools sharedTSNetTools] POST:@"https://www.yohomars.com/api/v2/comment/comments/commentlist?app_version=2.2.2&city_id=890&client_secret=79152d796d0528aca5ea1eef5287f60e&client_type=iphone&is_auth=0&lang=zh&limit=20&model=iPhone10%2C2&os_version=11.2.2&page=1&screen_size=414x736&session_code=a97cbc53113334edc9b42e56c1e6e08c&show_comments=1&type=0&udid=221198c14e9656e3b4f4fe080617b20e691cfe34&unique_token=221198c14e9656e3b4f4fe080617b20e691cfe34&v=2" params:nil isShowProgress:NO completion:^(NSError *error, id responseJson, BOOL isCache) {
        if (!error) {
            NSUInteger code = [responseJson[@"code"] integerValue];
            if (code == 200) {
                NSDictionary * data = responseJson[@"data"];
                NSArray * list = data[@"list"];
                NSArray * modelArray = [NSArray yy_modelArrayWithClass:[TSCityCommentModel class] json:list];
                [self formatCityCommentResult:modelArray];
               
                if (self.attachView && [self.attachView respondsToSelector:@selector(dataWithPresenter:didLoadMoreWithResult:errorArray:)]) {
                    [self.attachView dataWithPresenter:self didLoadMoreWithResult:self.mCityCommentArray errorArray:nil];
                }
            }
        }else
        {
            if (self.attachView && [self.attachView respondsToSelector:@selector(dataWithPresenter:didLoadMoreWithResult:errorArray:)]) {
                [self.attachView dataWithPresenter:self didLoadMoreWithResult:nil errorArray:@[error]];
            }
        }
    }];
}

- (void)fetchDataWithCompletionHandle:(NetCompletionHandle)completionHandle
{
    
    _mHeaderImageArray = [NSMutableArray array];
    _mHotAreaArray = [NSMutableArray array];
    _mCityCommentArray = [NSMutableArray array];

    kWeakSelf(self)
    [[TSNetTools sharedTSNetTools] groupPost:^{
        [weakself fetchHotAresData];
        [weakself fetchCityCommentData];
    } success:^{
        !completionHandle? :completionHandle(nil,nil,NO);
        if (self.attachView && [self.attachView respondsToSelector:@selector(dataWithPresenter:didRefreshWithResult:errorArray:)]) {
            [self.attachView dataWithPresenter:self didRefreshWithResult:nil errorArray:nil];
        }
    } failure:^(NSArray *errorArray) { //TODO:处理网络错误情况
         !completionHandle? :completionHandle((NSError *)errorArray[0],nil,NO);
        if (self.attachView && [self.attachView respondsToSelector:@selector(dataWithPresenter:didRefreshWithResult:errorArray:)]) {
            [self.attachView dataWithPresenter:self didRefreshWithResult:nil errorArray:errorArray];
        }
    }];
}

- (void)fetchHotAresData
{
    [[TSNetTools sharedTSNetTools] POST:@"https://www.yohomars.com/api/v1/bizarea/bizareas/index?app_version=2.2.2&city_id=890&client_secret=8b7efec54ee137a1d2164a76541c0f8d&client_type=iphone&lang=zh&limit_1=3&limit_2=6&model=iPhone10%2C2&os_version=11.2.2&radius=1&screen_size=414x736&session_code=a97cbc53113334edc9b42e56c1e6e08c&udid=221198c14e9656e3b4f4fe080617b20e691cfe34&unique_token=221198c14e9656e3b4f4fe080617b20e691cfe34&v=1" params:nil isShowProgress:NO completion:^(NSError *error, id responseJson, BOOL isCache) {
        if (!error) {
            NSUInteger code = [responseJson[@"code"] integerValue];
            if (code == 200) {
                NSDictionary * data = responseJson[@"data"];
                NSArray * list = data[@"list_2"];
                NSArray * modelArray = [NSArray yy_modelArrayWithClass:[TSHotAreaModel class] json:list];
//                NXLog(@"responseJson:%@",modelArray);
                [self formatHotAreaResult:modelArray];
            }else
            {
                
            }
        }
    }];
}

- (void)fetchCityCommentData
{
    [[TSNetTools sharedTSNetTools] POST:@"https://www.yohomars.com/api/v2/comment/comments/commentlist?app_version=2.2.2&city_id=890&client_secret=79152d796d0528aca5ea1eef5287f60e&client_type=iphone&is_auth=0&lang=zh&limit=20&model=iPhone10%2C2&os_version=11.2.2&page=1&screen_size=414x736&session_code=a97cbc53113334edc9b42e56c1e6e08c&show_comments=1&type=0&udid=221198c14e9656e3b4f4fe080617b20e691cfe34&unique_token=221198c14e9656e3b4f4fe080617b20e691cfe34&v=2" params:nil isShowProgress:NO completion:^(NSError *error, id responseJson, BOOL isCache) {
        if (!error) {
            NSUInteger code = [responseJson[@"code"] integerValue];
            if (code == 200) {
                NSDictionary * data = responseJson[@"data"];
                NSArray * list = data[@"list"];
                NSArray * modelArray = [NSArray yy_modelArrayWithClass:[TSCityCommentModel class] json:list];
//                NXLog(@"fetchCityCommentData====== responseJson:%@",modelArray);
                [self formatCityCommentResult:modelArray];
            }
        }else
        {
            
        }
    }];
}

#pragma mark - Utils

- (void)formatHotAreaResult:(NSArray *)result
{
    for (TSHotAreaModel * hotAreaModel in result) {
        TSHotAreaPresenter * hotAreaPresenter = [TSHotAreaPresenter instanceWithHotAreaModel:hotAreaModel];
        [self.mHotAreaArray addObject:hotAreaPresenter];
    }
}

- (void)formatCityCommentResult:(NSArray *)result
{
    for (TSCityCommentModel * commentModel in result) {
        TSCityCommentPresenter * presenter = [TSCityCommentPresenter instanceWithCommentModel:commentModel];
        [self.mCityCommentArray addObject:presenter];
    }
}

@end
